import { useDispatch } from 'umi';

const useDispatcher = <T>(reducerName: string = '', namespace = 'profile') => {
  const dispatch = useDispatch();
  return (newReducerName: string = '', payload?: T) => {
    const realReducerName = newReducerName || reducerName;
    if (!realReducerName) {
      throw new Error('reducer name is not empty');
    }
    return dispatch({
      type: `${namespace}/${realReducerName}`,
      payload,
    });
  };
};

export default useDispatcher;
